package com.karry.yl.service.system.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.base.impl.SuperServiceImpl;
import com.karry.commons.vo.Menu;
import com.karry.yl.dao.system.ResourcesMapper;
import com.karry.yl.dao.system.UserMapper;
import com.karry.yl.domain.baseinfo.Organization;
import com.karry.yl.domain.system.Resources;
import com.karry.yl.domain.system.User;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.system.LoginService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tianxuan
 * @version V1.0
 * @Date 2017/10/16 0016 上午 10:48
 * @Description: 登陆服务
 */
@Service
public class LoginServiceImpl extends SuperServiceImpl<UserMapper, User> implements LoginService {

    @Autowired
    private ResourcesMapper resourcesMapper;

    @Autowired
    private OrganizationService organizationService;

    /**
     * 根据用户名和密码 查询
     *
     * @param accountNumber
     * @param password
     * @return
     */
    @Override
    public User login(String accountNumber, String password) {
        password = DigestUtils.md5Hex(password);
        User user = null;
        if(!StringUtils.isEmpty(accountNumber)){
            char c = accountNumber.charAt(0);
            int i = (int) c;
            if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) {
                //是字母
                user = super.selectOne(new EntityWrapper<User>().eq("userCode", accountNumber).eq("password", password).eq("status", User.STATUS_ACTIVE).eq("deleted", false));
            } else {
                //不是字母
                user = super.selectOne(new EntityWrapper<User>().eq("phone", accountNumber).eq("password", password).eq("status", User.STATUS_ACTIVE).eq("deleted", false));
            }
        }
        return user;
    }

    @Override
    public List<Menu> menus(Long roleId) {
        List<Menu> menuList = new ArrayList<>();   //  菜单树集合
        Map<String, Menu> menuMap = new HashMap<>();  // map

        List<Resources> modules = resourcesMapper.findListByRole(roleId, Resources.TYPE_MODULE);   //查询出当前角色的所有模块
        List<Resources> pages = resourcesMapper.findListByRole(roleId, Resources.TYPE_PAGE);   //查询出当前角色的所有功能页面

        //将模块放入到菜单树
        for (Resources r : modules) {
            Menu menu = new Menu(r.getCode(), r.getName(), r.getUrl());  // 把模块权限对象 转化为模块菜单对象
            menu.setMainLogo(r.getMainLogo());
            menuList.add(menu);    //  把模块菜单对象 加入到菜单树集合
            menuMap.put(r.getCode(), menu);
        }

        //将页面放入到菜单树
        for (Resources r : pages) {
            Menu menu = new Menu(r.getCode(), r.getName(), r.getUrl().split(",")[0]);   // 把页面权限对象 转化为页面菜单对象
            Menu parentMenu = menuMap.get(r.getParentModule());
            if (null != parentMenu) {
                parentMenu.getSubmenus().add(menu);
            }
        }
        return menuList;
    }

    @Override
    public List<Resources> topMenus(Long roleId) {
        List<Resources> menuList = resourcesMapper.findListByRoleAndTopNav(roleId, Resources.TYPE_PAGE, 1);
        return menuList;
    }

    @Override
    public Map<String, Resources> myResources(Long roleId) {
        Map<String, Resources> resourcesMap = new HashMap<>();
        List<Resources> allResources = resourcesMapper.findAllListByRole(roleId);
        for (Resources p : allResources) {
            String url = p.getUrl();
            if (!StringUtils.isEmpty(url)) {
                String[] urls = url.split(",");
                for (String u : urls) {
                    resourcesMap.put(u, p);
                }
            }
        }
        return resourcesMap;
    }

    @Override
    public Map<String, List<String>> myPageResources(Long roleId) {
        Map<String, List<String>> pageResources = new HashMap<>();
        List<Resources> allResources = resourcesMapper.findAllListByRole(roleId);
        for (Resources p : allResources) {
            int type = p.getType();
            if (type == Resources.TYPE_SUB_PAGE || type == Resources.TYPE_OPT) {
                String parentPageCode = p.getParentPage();
                if (pageResources.containsKey(parentPageCode)) {
                    pageResources.get(parentPageCode).add(p.getCode());
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(p.getCode());
                    pageResources.put(parentPageCode, list);
                }
            }
        }
        return pageResources;
    }
}
